import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import mutations from './mutations'
import getters from './getters'
Vue.use(Vuex)

// 插件:没当mutations被提交代码就会执行
const cartPlugin = store => {
  store.subscribe((mutation, state) => {
    // 每当有mutations提交了，就会执行这个方法
    // 把state.cart存入localStorage
    localStorage.setItem('cart', JSON.stringify(state.cart))
  })
}

export default new Vuex.Store({
  plugins: [cartPlugin],
  state,
  mutations,
  getters
})
